package com.interesting.client.UI.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.interesting.R;
import com.interesting.base.core.BaseFragment;
import com.interesting.base.http.HttpUtil;
import com.interesting.client.UI.commonActivity.UIUtil;

/** 意见反馈 */
public class OpinionFragment extends BaseFragment {

	private EditText contentEdit, linkEdit;
	private Button send;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.setting_opinion_fragment, container, false);
		contentEdit = (EditText) v.findViewById(R.id.opinion_fragment_content);
		linkEdit = (EditText) v.findViewById(R.id.opinion_fragment_linkway);
		send = (Button) v.findViewById(R.id.opinion_fragment_send);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.opinion_fragment_send:
					String content = contentEdit.getText().toString();
					String link = linkEdit.getText().toString();
					submit(content, link);
					break;
				}
			}
		});
		super.onActivityCreated(savedInstanceState);

	}

	private void submit(String content, String link) {

		if (content != null && !content.equals("")) {
			new OptionAsync().execute(content, link);
		} else {
			UIUtil.showToast(getSherlockActivity(), "莫要惜字如金哦");
		}
	}

	/** 意见反馈 */
	private class OptionAsync extends AsyncTask<String, Void, JSONObject> {

		@Override
		protected JSONObject doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "ideaBack");
			request.put("content", params[0]);
			request.put("remark", params[1]);
			return HttpUtil.parseResult(HttpUtil.post(request));
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			if (result != null) {
				UIUtil.showToast(getSherlockActivity(), "非常感谢，反馈已收到！");
				getSherlockActivity().finish();
			}

			super.onPostExecute(result);
		}

	}
}
